﻿Public Class clsDBWbRackMng

    ''' <summary>
    ''' 指定したロットNoのテーブルを返却する
    ''' </summary>
    ''' <param name="LotNo">組立ロットNo</param>
    ''' <returns></returns>
    Public Function GetTableData(ByVal LotNo As String) As DataTable
        Dim szSql As String = ""

        szSql &= "SELECT  LOT_NO, STATUS, CONSTRUCTION_RACK, COMPLETION_RACK, EQUIP_ID, SHELF_NO, FRAME_NUM,ERR_INN_NUM,ERR_INN_FRM_NUM, LOCATION_CODE "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? "
        szSql &= "ORDER BY CONSTRUCTION_RACK "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo)
        If IsNothing(dt) Then
            GetTableData = New DataTable
        Else
            GetTableData = dt
        End If
    End Function
    Public Function GetAllData(ByVal LotNo As String) As DataTable
        Dim szSql As String = ""

        szSql &= "SELECT  * "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? "
        szSql &= "ORDER BY CONSTRUCTION_RACK "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo)
        If IsNothing(dt) Then
            GetAllData = New DataTable
        Else
            GetAllData = dt
        End If
    End Function
    ''' <summary>
    ''' 初回登録処理
    ''' </summary>
    ''' <param name="LotNo">ロットNo</param>
    ''' <param name="Status">状態(基本は着工前固定)</param>
    ''' <param name="ConstructionRackList">着工前ラックリスト</param>
    Public Sub InsertRackData(ByVal LotNo As String, ByVal Status As String, ByVal ConstructionRackList As List(Of String))
        Debug.WriteLine(System.Reflection.MethodBase.GetCurrentMethod.Name + " in")
        Debug.WriteLine(String.Format("lotno:{0} status:{1} ", LotNo, Status))
        Dim szSql As String = ""
        szSql &= "INSERT INTO PER.T_WB_RACK_MNG( "
        szSql &= "  LOT_NO, STATUS, CONSTRUCTION_RACK, COMPLETION_RACK, EQUIP_ID, SHELF_NO, FRAME_NUM,ERR_INN_NUM,ERR_INN_FRM_NUM, LOCATION_CODE "
        szSql &= ") "
        szSql &= "VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "

        For Each rackNo In ConstructionRackList
            Debug.WriteLine(String.Format("rackNo:{0}", rackNo))
            If Not String.IsNullOrEmpty(rackNo) Then 'ADD 苗 2018/2/7 「1ラックだけの場合、システムエラーが発生」の対応
                DBacs.ExecuteUpdateSql(szSql, -1, LotNo, Status, rackNo, "", "", "", "", "", "", "")
            End If
        Next


        'ADD 苗 2018/3/17 BEGIN
        szSql = ""
        szSql &= "DELETE FROM PER.T_WB_RACK_MNG WHERE LOT_NO = ? AND TRIM(CONSTRUCTION_RACK) = '' "
        DBacs.ExecuteUpdateSql(szSql, -1, LotNo)
        'ADD 苗 2018/3/17 END

    End Sub

    ''' <summary>
    ''' 更新処理
    ''' </summary>
    ''' <param name="LotNo">ロットNo</param>
    ''' <param name="Status">状態</param>
    ''' <param name="ConstructionRack">着工前ラックNo</param>
    ''' <param name="CompletionRack">完了ラックNo</param>
    ''' <param name="EquipId">装置ID</param>
    ''' <param name="ShelfNo">棚番号</param>
    ''' <param name="FrameNum">フレーム数</param>
    ''' <param name="ErrInnNum">×印数</param>'ADD 苗 2018/1/6
    ''' <param name="ErrInnFrmNum">×印枚数</param>'ADD 苗 2018/1/6
    ''' <param name="LocationCode">場所コード</param>
    Public Sub UpdateRackData(ByVal LotNo As String, ByVal Status As String, ByVal ConstructionRack As String, CompletionRack As String,
                               EquipId As String, ShelfNo As String, FrameNum As String, ErrInnNum As String, ErrInnFrmNum As String, LocationCode As String)
        Debug.WriteLine(System.Reflection.MethodBase.GetCurrentMethod.Name + " in")
        Debug.WriteLine(String.Format("lotno:{0} status:{1} ConsRack:{2} CompRack:{3} ", LotNo, Status, ConstructionRack, CompletionRack))
        Debug.WriteLine(String.Format("EquipId:{0} ShelfNo:{1} FrameNum:{2} LocationCode:{3} ErrInnNum:{4} ErrInnFrmNum:{5}", EquipId, ShelfNo, FrameNum, LocationCode, ErrInnNum, ErrInnFrmNum))
        Dim szSql As String = ""
        Dim m_cData As New clsDbCondData
        szSql &= "UPDATE PER.T_WB_RACK_MNG "
        szSql &= " SET STATUS = ?, COMPLETION_RACK = ?, EQUIP_ID = ?, SHELF_NO = ?, FRAME_NUM = ?, err_inn_num = ?,err_inn_frm_num = ?,LOCATION_CODE = ? "
        If Status = "1" Then
            szSql &= ",MOVEINTIME ='" & m_cData.GetServerTime() & "'"
        ElseIf Status = "2" Then
            szSql &= ",MOVESTDTIME ='" & m_cData.GetServerTime() & "'"
        End If
        szSql &= " WHERE LOT_NO = ? AND CONSTRUCTION_RACK = ? ;"
        DBacs.ExecuteUpdateSql(szSql, -1, Status, CompletionRack, EquipId, ShelfNo, FrameNum, ErrInnNum, ErrInnFrmNum, LocationCode, LotNo, ConstructionRack)
    End Sub

    ''' <summary>
    ''' 着工前ラックの状態確認
    ''' </summary>
    ''' <param name="LotNo">ロットNo</param>
    ''' <param name="Status">状態</param>
    ''' <param name="RackNo">着工前ラックNo</param>
    ''' <returns></returns>
    Public Function IsCheckRackStatus(ByVal LotNo As String, ByVal Status As String, ByVal RackNo As String, ByVal EquipId As String) As Boolean
        Dim szSql As String = ""
        If LotNo = "" Then Return True
        szSql &= "SELECT  STATUS, EQUIP_ID "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? AND CONSTRUCTION_RACK = ? "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo, RackNo)
        If dt.Rows.Count > 0 Then
            For Each item In dt.Rows
                If item("STATUS") = Status OrElse DBNullTOblank(item("EQUIP_ID")) = EquipId Then
                    Return True
                End If
            Next
        End If
        Return False
    End Function


    Public Function chkRackStatus(ByVal LotNo As String, ByVal Status As String, ByVal RackNo As String, ByVal EquipId As String) As Boolean
        Dim szSql As String = ""
        If LotNo = "" Then Return True
        szSql &= "SELECT  STATUS "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? AND CONSTRUCTION_RACK = ? "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo, RackNo)
        If dt.Rows.Count > 0 Then
            For Each item In dt.Rows
                If item("STATUS") = Status Then
                    Return True
                End If
            Next
        End If
        Return False
    End Function

    Public Function chkRackAuto(ByVal LotNo As String, ByVal RackNo As String) As Boolean
        Dim szSql As String = ""
        Dim rtn As Boolean = True
        If LotNo = "" Then Return True

        szSql &= "SELECT  FRAME_NUM "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? AND CONSTRUCTION_RACK = ? "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo, RackNo)
        If dt.Rows.Count > 0 Then
            For Each item In dt.Rows
                If IsDBNull(item("FRAME_NUM")) OrElse item("FRAME_NUM") = "" Then
                    rtn = True
                ElseIf item("FRAME_NUM") = "2" Then
                    rtn = False
                ElseIf item("FRAME_NUM") = "1" Then
                    rtn = True
                End If

            Next
        End If
        Return rtn
    End Function

    ''' <summary>
    ''' 完了ラックNoの重複チェック
    ''' </summary>
    ''' <param name="LotNo"></param>
    ''' <param name="CompRackNo"></param>
    ''' <param name="EquipId"></param>
    ''' <returns></returns>
    Public Function IsCheckCompRack(ByVal LotNo As String, ByVal CompRackNo As String, ByVal EquipId As String, ByVal Status As String) As Boolean
        Dim szSql As String = ""

        szSql &= "SELECT  STATUS, EQUIP_ID "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? AND COMPLETION_RACK = ? "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo, CompRackNo)
        If dt.Rows.Count > 0 Then
            For Each item In dt.Rows
                If item("STATUS") <> Status Then
                    Return False
                End If
            Next
        End If
        Return True
    End Function

    ''' <summary>
    ''' 棚番号の重複チェック
    ''' </summary>
    ''' <param name="LotNo"></param>
    ''' <param name="ShelfNo"></param>
    ''' <returns></returns>
    Public Function IsCheckShelfNo(ByVal LotNo As String, ByVal ShelfNo As String) As Boolean
        Dim szSql As String = ""
        Return True
        szSql &= "SELECT  SHELF_NO "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? AND NOT SHELF_NO IS NULL " 'UPD　MIAO 2018/10/22

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo)
        If dt.Rows.Count > 0 Then
            For Each item In dt.Rows
                If item("SHELF_NO") = ShelfNo Then
                    Return True
                End If
            Next
            Return False
        End If
        Return True
    End Function

    ''' <summary>
    ''' 棚番号を取得する
    ''' </summary>
    ''' <param name="LotNo">ロットNo</param>
    ''' <returns></returns>
    Public Function getShelfNo(ByVal LotNo As String) As String
        Dim szSql As String = ""

        szSql &= "SELECT  SHELF_NO "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? AND NOT SHELF_NO IS NULL " 'UPD　MIAO 2018/10/22

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo)
        If dt.Rows.Count > 0 Then
            For Each item In dt.Rows
                Return item("SHELF_NO")
            Next
        End If
        Return String.Empty
    End Function

    ''' <summary>
    ''' 作業場所を取得する
    ''' </summary>
    ''' <param name="LotNo">ロットNo</param>
    ''' <returns></returns>
    Public Function getLocationCode(ByVal LotNo As String) As String
        Dim szSql As String = ""

        szSql &= "SELECT  LOCATION_CODE "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? AND NOT LOCATION_CODE IS NULL " 'UPD　MIAO 2018/10/22

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo)
        If dt.Rows.Count > 0 Then
            For Each item In dt.Rows
                Return item("LOCATION_CODE")
            Next
        End If
        Return String.Empty
    End Function

    ''' <summary>
    ''' 指定ロット内のラック全てがStatusと一致しているか
    ''' </summary>
    ''' <param name="LotNo">ロットNo</param>
    ''' <param name="Status">確認ステータス</param>
    ''' <returns></returns>
    Public Function IsCompleteStatus(ByVal LotNo As String, ByVal Status As String, ByVal RackNo As String) As Boolean
        Dim szSql As String = ""

        szSql &= "SELECT  STATUS, CONSTRUCTION_RACK "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo)
        If dt.Rows.Count > 0 Then
            For Each item In dt.Rows
                If item("STATUS") <> Status Then
                    If item("CONSTRUCTION_RACK") <> RackNo Then
                        Return False
                    End If
                End If
            Next
            Return True
        End If
        Return False
    End Function

    '>>20180329 大賀 No125 S42で最後の完成を実施しても臨時完成となる場合がある
    ''' <summary>
    ''' bSiteへの完成報告の可否を取得する
    ''' </summary>
    ''' <param name="LotNo">ロットNo</param>
    ''' <param name="RackList">ラックNoリスト</param>
    ''' <returns></returns>
    Public Function CheckCompleteStatus(ByVal LotNo As String, ByVal RackList As List(Of String)) As frmStartCompleteBase.SendStatus
        Dim szSql As String = ""
        Dim ret = frmStartCompleteBase.SendStatus.OK

        szSql &= "SELECT  STATUS, CONSTRUCTION_RACK "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo)
        If dt.Rows.Count > 0 Then
            For Each item In dt.Rows
                If item("STATUS") = frmStartCompleteBase.WB_BSITE_SENDING Then
                    Return frmStartCompleteBase.SendStatus.SENDING
                ElseIf item("STATUS") <> frmStartCompleteBase.TagProcessStatus.ManufComplete Then
                    ' >> 20180712 佐川 ラックID入力追加
                    'If item("CONSTRUCTION_RACK") <> RackList(0) Then
                    If RackList.FindIndex(Function(x) x = item("CONSTRUCTION_RACK")) = -1 Then
                        ret = frmStartCompleteBase.SendStatus.NG
                    End If
                    ' << 20180712 佐川 ラックID入力追加
                End If
            Next
        Else
            ret = frmStartCompleteBase.SendStatus.NG
        End If
        Return ret
    End Function
    '<<20180329 大賀 No125 S42で最後の完成を実施しても臨時完成となる場合がある

    ' 表示部のStringをIntegerに変換する
    Private Function convertInteger(inStr As String) As Integer
        Dim ret As Integer = 0
        If Not String.IsNullOrEmpty(inStr) Then
            ret = CInt(inStr)
        End If
        Return ret
    End Function

    ''' <summary>
    ''' 指定ロット内のフレーム数の合計値を取得
    ''' </summary>
    ''' <param name="LotNo">ロットNo</param>
    ''' <returns></returns>
    Public Function getTotalFrame(ByVal LotNo As String) As String
        Dim szSql As String = ""

        szSql &= "SELECT  FRAME_NUM "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? "
        Dim sum = 0
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo)
        If dt.Rows.Count > 0 Then
            For Each item In dt.Rows
                sum += 1 'convertInteger(item("FRAME_NUM"))
            Next
        End If
        Return sum.ToString
    End Function

    ''' <summary>
    ''' 指定設備IDのStatusを取得
    ''' </summary>
    ''' <param name="LotNo">ロットNo</param>
    ''' <param name="EquipId">設備ID</param>
    ''' <returns></returns>
    Public Function getEquipIdStatus(ByVal LotNo As String, ByVal EquipId As String) As String
        Dim szSql As String = ""

        szSql &= "SELECT  STATUS "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? AND EQUIP_ID = ? "
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo, EquipId)
        If dt.Rows.Count > 0 Then
            For Each item In dt.Rows
                Return item("STATUS")
            Next
        End If
        Return "0"  ' 着工前
    End Function
    'ADD 苗 2018/1/6 BEGIN
    ''' <summary>
    ''' 指定設備IDのStatusを取得（１設備複数ラックの場合）
    ''' </summary>
    ''' <param name="LotNo">ロットNo</param>
    ''' <param name="EquipId">設備ID</param>
    ''' <returns></returns>
    Public Function getStatusForOneEquip(ByVal LotNo As String, ByVal EquipId As String) As String
        Dim szSql As String = ""

        szSql &= "SELECT  MIN(STATUS) AS STATUS ,COUNT(STATUS) AS COUNT "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? AND EQUIP_ID = ? "
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo, EquipId)
        If dt.Rows.Count > 0 Then
            For Each item In dt.Rows
                If item("COUNT") > 0 Then
                    Return item("STATUS")
                Else
                    Return "0"
                End If
            Next
        End If
        Return "ERR"  ' 着工前
    End Function

    '''' <summary>
    '''' lotの最後ラックの判断
    '''' </summary>
    '''' <param name="LotNo">ロットNo</param>
    '''' <returns></returns>
    'Public Function isLastRack(ByVal LotNo As String) As Boolean
    '    Dim szSql As String = ""

    '    szSql &= "SELECT COUNT(STATUS) as cmpRackCnt FROM PER.T_WB_RACK_MNG "
    '    szSql &= "WHERE LOT_NO = ? AND STATUS='2'  "
    '    Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo)
    '    If dt.Rows.Count > 0 Then
    '        For Each item In dt.Rows
    '            If 5 - item("cmpRackCnt") = 1 Then
    '                Return True
    '            Else
    '                Return False
    '            End If
    '        Next
    '    End If
    '    Return False
    'End Function
    ''' <summary>
    ''' 廃棄フレーム数、X印数、X印枚数
    ''' </summary>
    ''' <param name="LotNo">ロットNo</param>
    ''' <param name="SUM_ERR_INN_NUM">X印数の合計</param>
    ''' <param name="SUM_ERR_INN_FRM_NUM">X印枚数の合計</param>
    ''' <param name="SUM_FRAME_NUM">廃棄フレーム数の合計</param>
    ''' <returns></returns>
    ''' 
    Public Function getSum(ByVal LotNo As String, ByRef SUM_ERR_INN_NUM As Integer, ByRef SUM_ERR_INN_FRM_NUM As Integer, ByRef SUM_FRAME_NUM As Integer) As Boolean
        Dim szSql As String = ""

        szSql &= "SELECT FRAME_NUM,ERR_INN_NUM, ERR_INN_FRM_NUM "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? "
        Dim sum = 0
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo)
        If dt.Rows.Count > 0 Then

            For Each item In dt.Rows
                If DBNullTOblank(item("ERR_INN_NUM")) <> "" Then SUM_ERR_INN_NUM = SUM_ERR_INN_NUM + DBNullTOblank(item("ERR_INN_NUM")) 'X印数 'UPD MIAO 2018/10/22
                If DBNullTOblank(item("ERR_INN_FRM_NUM")) <> "" Then SUM_ERR_INN_FRM_NUM = SUM_ERR_INN_FRM_NUM + DBNullTOblank(item("ERR_INN_FRM_NUM")) 'X印枚数 'UPD MIAO 2018/10/22
                If DBNullTOblank(item("FRAME_NUM")) <> "" Then SUM_FRAME_NUM = SUM_FRAME_NUM + DBNullTOblank(item("FRAME_NUM")) 'UPD MIAO 2018/10/22
            Next
            Return True
        End If
        Return False
    End Function
    'ADD 苗 2018/1/6 END

    'ADD 苗 2018/1/15 BEGIN 追加前、削除する
    ''' <summary>
    ''' 初回登録前の旧データの削除処理
    ''' </summary>
    ''' <param name="LotNo">ロットNo</param>
    Public Sub delOldData(ByRef lotNo As String)

        Dim szSql As String = ""
        szSql &= "DELETE FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? ;"
        DBacs.ExecuteUpdateSql(szSql, -1, lotNo)

    End Sub
    'ADD 苗 2018/1/15 END 追加前、削除する

    ' >> 20180309 大賀 着工中のデータをWBラック管理テーブルから取得するように修正
    ''' <summary>
    ''' ラック管理テーブルから指定設備の明細データを取得する
    ''' </summary>
    ''' <param name="LotNo">ロットNo</param>
    ''' <param name="Status">確認ステータス</param>
    ''' <param name="EquipId">設備ID</param>
    ''' <returns></returns>
    Public Function getDetailData(ByVal LotNo As String, ByVal Status As String, ByVal EquipId As String) ', Optional rackNo As String = "") As DataTable
        Dim szSql As String = ""

        szSql &= "SELECT  LOT_NO, STATUS, CONSTRUCTION_RACK, COMPLETION_RACK, EQUIP_ID, SHELF_NO, FRAME_NUM, ERR_INN_NUM, ERR_INN_FRM_NUM, LOCATION_CODE "
        szSql &= "FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? AND STATUS = ? AND EQUIP_ID = ? "
        'If rackNo <> "" Then
        '    szSql &= " AND CONSTRUCTION_RACK='" & rackNo & "'"

        'End If
        szSql &= "ORDER BY CONSTRUCTION_RACK "

        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo, Status, EquipId)
        ' >> 20180712 佐川 ラックID入力追加
        'If IsNothing(dt) Then
        If dt.Rows.Count < 0 Then
            ' << 20180712 佐川 ラックID入力追加
            getDetailData = Nothing
        Else
            getDetailData = dt
        End If
    End Function
    ' << 20180309 大賀 着工中のデータをWBラック管理テーブルから取得するように修正
    'ADD MIAO 2018/10/17 Begin

    'ADD MIAO 2018/10/17 END
    '棚アドレス検索
    Public Function GetShelfAdrFC(ByVal kLotno As String) As String
        Dim RTN As String = ""
        Dim szSql = "    SELECT shwlfno "
        szSql = szSql & "FROM PER.L_STORAGE_IO "
        szSql = szSql & "WHERE  klotno = '" & kLotno & "' "
        Dim dt As New DataTable

        dt = DBacs.ExecuteSql(szSql)

        If dt.Rows.Count = 0 Then
            RTN = ""
        Else
            RTN = dt.Rows(0)(0).ToString
        End If
        Return RTN

    End Function

    Public Function getAutoMode(ByVal LotNo As String, ByRef inRackNo As String, ByRef outRackNo As String, equip As String) As String
        Dim szSql As String = ""
        Dim rtn As String = ""
        If inRackNo <> "" Then
            szSql = ""
            szSql &= "SELECT FRAME_NUM,CONSTRUCTION_RACK,COMPLETION_RACK  "
            szSql &= "FROM PER.T_WB_RACK_MNG "
            szSql &= "WHERE LOT_NO = ? AND CONSTRUCTION_RACK = ? "

        Else
            szSql = ""
            szSql &= "SELECT FRAME_NUM,CONSTRUCTION_RACK,COMPLETION_RACK  "
            szSql &= "FROM PER.T_WB_RACK_MNG "
            szSql &= "WHERE LOT_NO = ? AND EQUIP_ID = ? AND STATUS='1'"
        End If


        Dim sum = 0
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, LotNo, equip)
        If dt.Rows.Count > 0 Then
            rtn = IIf(IsDBNull(dt.Rows(0)(0)), "", dt.Rows(0)(0))
            inRackNo = IIf(IsDBNull(dt.Rows(0)(1)), "", dt.Rows(0)(1))
            outRackNo = IIf(IsDBNull(dt.Rows(0)(2)), "", dt.Rows(0)(2))
        End If
        Return rtn
    End Function

    Public Function delRackNo(lotno As String, inRack As String) As Integer
        Dim szSql As String = ""
        szSql &= "DELETE FROM PER.T_WB_RACK_MNG "
        szSql &= "WHERE LOT_NO = ? AND CONSTRUCTION_RACK = ?"
        delRackNo = DBacs.ExecuteUpdateSql(szSql, -1, lotno, inRack)
        Dim objlotDefect As New clsDbLotDefect
        objlotDefect.delRack(lotno, inRack)
    End Function

    Public Function updateRackNo(lotno As String, dr As DataRow, ope As String) As Integer
        Dim szSql As String = ""
        szSql &= "update PER.T_WB_RACK_MNG "
        szSql &= "set STATUS = ?,CONSTRUCTION_RACK=?,COMPLETION_RACK=? ,EQUIP_ID=?,UPDATE_OPE=? "
        szSql &= "where LOT_NO=? and CONSTRUCTION_RACK=? "

        updateRackNo = DBacs.ExecuteUpdateSql(szSql, -1, dr("STATUS"), dr("CONSTRUCTION_RACK"), dr("COMPLETION_RACK"), dr("EQUIP_ID"),
                                     ope, lotno, dr("CONSTRUCTION_RACK", DataRowVersion.Original))

        Dim objlotDefect As New clsDbLotDefect
        objlotDefect.setNewRack(lotno, dr("CONSTRUCTION_RACK", DataRowVersion.Original), dr("CONSTRUCTION_RACK"))

    End Function

    Public Function insertRackNo(lotno As String, dr As DataRow, ope As String) As Integer
        Dim szSql As String = ""
        szSql &= "INSERT INTO PER.T_WB_RACK_MNG( "
        szSql &= "LOT_NO,STATUS,CONSTRUCTION_RACK,COMPLETION_RACK ,EQUIP_ID,UPDATE_OPE) values (?,?,?,?,?,?)"
        insertRackNo = DBacs.ExecuteUpdateSql(szSql, -1,
                       lotno, dr("STATUS"), DBNullTOblank(dr("CONSTRUCTION_RACK")), DBNullTOblank(dr("COMPLETION_RACK")), DBNullTOblank(dr("EQUIP_ID")), ope)
        Dim objlotDefect As New clsDbLotDefect
        objlotDefect.insertRackInfo(lotno, "S42", dr("CONSTRUCTION_RACK"), "", "", "", "")
    End Function

    Public Function saveChangedValue(lotno As String, ope As String, ByRef dt As DataTable) As Integer
        Dim RTN As Integer = 0
        DBacs.BeginTransaction()
        Try
            For Each dr As DataRow In dt.GetChanges.Rows
                If dr.RowState = DataRowState.Added Then
                    If DBNullTOblank(dr("Status")) = "" Then dr("Status") = "0"
                    If insertRackNo(lotno, dr, ope) = -2 Then
                        RTN = -2
                        Exit For
                    End If
                End If
                If dr.RowState = DataRowState.Modified Then
                    If updateRackNo(lotno, dr, ope) = -2 Then
                        RTN = -2
                        Exit For
                    End If
                End If
                If dr.RowState = DataRowState.Deleted Then
                    If delRackNo(lotno, DBNullTOblank(dr("CONSTRUCTION_RACK", DataRowVersion.Original))) = -2 Then
                        RTN = -2
                        Exit For
                    End If
                End If
            Next
            '更新IDTAG
            Dim CompletedFlg As Boolean = True
            For Each dr As DataRow In dt.Rows
                If Not (dr.RowState = DataRowState.Deleted) Then
                    If DBNullTOblank(dr("Status")) <> "2" Then
                        CompletedFlg = False
                        Exit For
                    End If
                End If
            Next

            Dim rackString As String = ""

            For i As Integer = 0 To dt.Rows.Count - 1
                If Not (dt(i).RowState = DataRowState.Deleted) Then
                    If CompletedFlg = False Then
                        rackString = rackString & DBNullTOblank(dt(i)("CONSTRUCTION_RACK")) & "%"
                    Else
                        rackString = rackString & DBNullTOblank(dt(i)("COMPLETION_RACK")) & "%"
                    End If
                End If
            Next
            rackString = rackString.Substring(0, rackString.Length - 1)
            Dim rack() As String = rackString.Split("%")

            Dim szSql As String = ""
            szSql &= "update PER.L_TAG_LOTDATA "
            szSql &= "set CONTAINER1 = ?,CONTAINER2=?,CONTAINER3=? ,CONTAINER4=?,CONTAINER5=? "
            szSql &= "where LOT_NO=?  "
            Dim updRtn As Integer = 0
            If rack.Length = 1 Then
                updRtn = DBacs.ExecuteUpdateSql(szSql, -1, rack(0), "", "", "", "", lotno)
            End If
            If rack.Length = 2 Then
                updRtn = DBacs.ExecuteUpdateSql(szSql, -1, rack(0), rack(1), "", "", "", lotno)
            End If
            If rack.Length = 3 Then
                updRtn = DBacs.ExecuteUpdateSql(szSql, -1, rack(0), rack(1), rack(2), "", "", lotno)
            End If
            If rack.Length = 4 Then
                updRtn = DBacs.ExecuteUpdateSql(szSql, -1, rack(0), rack(1), rack(2), rack(3), "", lotno)
            End If
            If rack.Length = 5 Then
                updRtn = DBacs.ExecuteUpdateSql(szSql, -1, rack(0), rack(1), rack(2), rack(3), rack(4), lotno)
            End If

            If updRtn = -2 Then
                RTN = -2
            End If
            If RTN = 0 Then
                dt.AcceptChanges()
                DBacs.CommitTransaction()
            Else
                DBacs.RollbackTransaction()
            End If
        Catch ex As Exception
            DBacs.RollbackTransaction()
            RTN = -2
        End Try
        Return RTN

    End Function
    Public Function DBNullTOblank(obj As Object) As String
        DBNullTOblank = IIf(IsDBNull(obj), "", obj)
    End Function
End Class
